var that;
var Util = require('../../utils/util.js');
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');

var app = getApp();
Page({
  data: {
    titletext: "",
    modalHidden: true,
    inputphone: "",
    inputpassWord: "",
    openid: "",
    disabled: false,
    hidden: true,
    picture: "http://shop.app.queyouquan.cn/assets/bg_integral.png",
  },
  onLoad: function () {
    var that = this;
    if (!app.getOpenID()) {
      // 获取配置
      app.getConfigData(app.getCode);
    }
  },
  onShow: function () {
    wx.getSystemInfo({
      success: function (res) {
        let num = res.model.slice(0,8)
        if (num == "iPhone X") {
          GlobalData.isshowiphone = true
        } else {
          GlobalData.isshowiphone = false
        }
      }
    })
    if (app.getPhone()){
      this.setData({
        inputphone: app.getPhone()
      })
    }
    if (app.getPassword()){
      this.setData({
        inputpassWord: app.getPassword()
      })
    }
    if (app.getConfig() && app.getConfig().isFormal) {
      this.setData({
        picture: "http://shop.app.queyouquan.cn/assets/bg_shop.png"
      })
    }
    this.setData({
      hidden: true
    })
    GlobalData.shopcar_1 = [];
    GlobalData.shopcar_2 = [];
    dataManager.removeBtnObserver("login")
    if (GlobalData.phoneNumber)
      this.setData({
        inputphone: GlobalData.phoneNumber
      })
    if (GlobalData.passWord)
      this.setData({
        inputpassWord: GlobalData.passWord
      })
    if (GlobalData.phoneNumber && GlobalData.passWord) {
      this.setData({
        disabled: true
      })
    }

  },
  onLogin: function () {
    var that = this;
    if (app.getConfig()) {
      // that.setData({
      //   inputphone: 18380582455,
      //   inputpassWord: 123456
      // })
      //  账号密码是否为空
      if (this.data.inputphone == undefined || this.data.inputphone == null || this.data.inputphone == 0) {
        app.onTip("请输入正确的账号。")
        return
      }
      //  账号密码是否为空
      if (this.data.inputpassWord == undefined || this.data.inputpassWord == null || this.data.inputpassWord == "") {
        app.onTip("请输入正确的密码。")
        return
      }
      let dataMsg = { method: "CodeLogin", openid: app.getOpenID(), mobile: this.data.inputphone, password: this.data.inputpassWord }
      let url = app.getRequestUrl(app.getConfig().isFormal);
      // let url = "https://dev.cardshopapi.queyouquan.net/API/Agent.aspx"
      wx.request({
        url: url,
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: Util.json2Form(dataMsg),
        complete: function (res) {
          let pkg = res.data;
          if (res && pkg && pkg.errcode == 0) {
            GlobalData.phoneNumber = that.data.inputphone;
            GlobalData.passWord = that.data.inputpassWord;
            app.setPhone(that.data.inputphone);
            app.setPassword(that.data.inputpassWord);
            app.setToken(pkg.data.token);
            if (app.getConfig().isFormal) {
              dataManager.UseNavigateTo("/pages/component/homes/home/home", true);
            } else
              dataManager.UseNavigateTo("/pages/integralmall/integralmall", true);
          } else {
            if (pkg.errcode == 402) {
              that.data.inputphone = 0;
            }
            if (pkg.errcode == 403) {
              that.data.inputpassWord = ""
            }
            app.show(pkg.errmsg)

          }
        }
      })
    }
  },
  register: function () {

    // this.setData({
    //   titletext: "游戏内申请或通过其他代理的推广链接可申请成为代理。"
    // })
    if (app.getConfig().isFormal) {
      getApp().onTip("游戏内申请或通过其他代理的推广链接可申请成为代理。");
    }

  },

  //获取手机号码 
  bindKeyInput: function (e) {
    var num = parseInt(e.detail.value)
    if (num == 0) {
      return "";
    }
    this.setData({
      inputphone: num
    })
  },
  //获取密码
  bindPassWordInput: function (e) {
    // console.log(e, "密码 ");
    var code = e.detail.value.trim();
    this.setData({
      inputpassWord: code
    })
  },
  bindPass: function (e) {
    this.setData({
      hidden: false
    })
    wx.navigateTo({
      url: "/pages/component/logins/forgetPassword/forgetPassword"
    })
  }
})

